package com.example.travel_front.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.travel_front.Adapter.ChatAdapter;
import com.example.travel_front.Model.Chat;
import com.example.travel_front.R;

import java.util.ArrayList;
import java.util.List;

public class ChatFragment extends Fragment implements ChatAdapter.OnChatClickListener{


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

        // 示例数据
        List<Chat> chatList = getChatList();
        ChatAdapter adapter = new ChatAdapter(chatList, this);
        recyclerView.setAdapter(adapter);

        return view;
    }

    @Override
    public void onChatClick(Chat chat) {
        MessageFragment messageFragment = MessageFragment.newInstance(chat.getId());
        // 执行 Fragment 事务
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, messageFragment);
        transaction.addToBackStack(null); // 可以回退到当前 Fragment
        transaction.commit();
    }
    private List<Chat> getChatList() {
        // TODO: 实现从数据库或API获取护理之家列表的逻辑
        List<Chat> chats = new ArrayList<>();
        chats.add(new Chat(
                "Yitian Nursing Home",
                "Hello, how can we help you today?",
                "12:11",
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG541.jpg",
                1));

        chats.add(new Chat(
                "Green Valley Care",
                "We have received your application.",
                "Yesterday",
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG542.jpg",
                2));

        chats.add(new Chat(
                "Sunrise Senior Living",
                "Your scheduled visit is tomorrow at 10:00 AM.",
                "Two days ago",
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG543.jpg",
                3));

        chats.add(new Chat(
                "Harmony Senior Services",
                "Can you please confirm your appointment?",
                "12/10/2022",
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG544.jpg",
                4));

        chats.add(new Chat(
                "Harmony Senior Services",
                "Can you please confirm your appointment?",
                "12/10/2022",
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG545.png",
                5));
        return chats;
    }

}
